package com.ccl.iot.publics;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.List;
import java.util.regex.Pattern;

import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Debug;
import android.os.Process;
import android.provider.Settings.System;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

import com.ccl.iot.App;

public class SystemInfo {
	private static int gCPUCores = 0;

	static class Filter implements FileFilter {
		String iFilterExpression = null;

		Filter(String aFilterExpression) {
			iFilterExpression = aFilterExpression;
		}

		@Override
		public boolean accept(File arg0) {
			if (Pattern.matches(iFilterExpression, arg0.getName())) {
				return true;
			} else {
				return false;
			}
		}
	}

	public static int GetCPUCores() {
		if (gCPUCores == 0) {
			try {
				File tCPU = new File("/sys/devices/system/cpu/");

				File tCPUs[] = tCPU.listFiles(new Filter("cpu[0-64"));

				gCPUCores = tCPUs.length;
			} catch (Exception e) {
				e.printStackTrace();

				gCPUCores = 1;
			}
		}

		return gCPUCores;
	}

	/*
	 * Get CPU serial number
	 */
	private static String iCPUSerial = null;

	public static String GetCPUSerial() {
		if (iCPUSerial == null) {
			String str = "", strCPU = "";
			try {
				// read CPU information
				java.lang.Process tProcess = Runtime.getRuntime().exec("cat /proc/cpuinfo");
				InputStreamReader tReader = new InputStreamReader(tProcess.getInputStream());
				LineNumberReader input = new LineNumberReader(tReader);

				// Find CPU serial number
				for (int i = 1; i < 100; i++) {
					str = input.readLine();
					if (str != null) {
						// Find CPU serial number line
						if (str.indexOf("Serial") > -1) {
							// Get serial number from line
							strCPU = str.substring(str.indexOf(":") + 1, str.length());

							iCPUSerial = strCPU.trim();
							break;
						}
					} else {
						// End of the stream
						break;
					}
				}

				tProcess.destroy();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		return iCPUSerial;
	}

	private static String gIMEI = null;

	public static String GetIMEI(Context context) {
		if (gIMEI == null && context != null) {
			gIMEI = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
		}

		return gIMEI;
	}

	private static String gAndroidID = null;

	public static String GetAndroidID(Context context) {
		if (gAndroidID == null && context != null) {
			gAndroidID = System.getString(context.getContentResolver(), System.ANDROID_ID);
		}

		return gAndroidID;
	}

	private static String gDeviceID = null;

	/*
	 * Get device uniqueness ID
	 */
	public static String GetDeviceID(Context context) {
		// 1.Get device IMEI number
		// Add permission to Manifest: <uses-permission
		// android:name="android.permission.READ_PHONE_STATE" />
		if (gDeviceID == null) {
			if (context != null) {
				gDeviceID = GetIMEI(context);
				if (gDeviceID == null) {
					// 2.ANDROID_ID
					gDeviceID = GetAndroidID(context);
					if (gDeviceID == null) {
						// 3.CPU Serial number
						gDeviceID = SystemInfo.GetCPUSerial();
					}
				}
			}
		}

		return gDeviceID;
	}

	public static String[] GetSystemInfoStrings() {
		return new String[] { android.os.Build.BOARD, android.os.Build.BOOTLOADER, android.os.Build.BRAND, android.os.Build.CPU_ABI, android.os.Build.CPU_ABI2, android.os.Build.DEVICE,
				android.os.Build.FINGERPRINT, android.os.Build.HARDWARE, android.os.Build.HOST, android.os.Build.ID, android.os.Build.MANUFACTURER, android.os.Build.MODEL, android.os.Build.PRODUCT,
				android.os.Build.RADIO, android.os.Build.SERIAL };
	}

	public static int GetScreenSize(Context aContext) {
		return aContext.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
	}

	public static int GetMenuIconSize() {
		DisplayMetrics tMetrics = Resources.getSystem().getDisplayMetrics();
		int tSize;

		tMetrics.setToDefaults();
		switch (tMetrics.densityDpi) {
		case DisplayMetrics.DENSITY_HIGH: {
			tSize = 48;

			break;
		}
		case DisplayMetrics.DENSITY_XHIGH: {
			tSize = 72;

			break;
		}
		default: {
			tSize = 32;
		}
		}

		return tSize;
	}

	public static long GetSystemMemoryFree() {
		return Runtime.getRuntime().freeMemory();
	}

	public static long GetCurrentProcessMemoryUsed(Context context) {
		return GetProcessMemoryUsed(context, Process.myPid());
	}

	public static long GetProcessMemoryUsed(Context context, int aPID) {
		ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

		List<ActivityManager.RunningAppProcessInfo> appProcessList = mActivityManager.getRunningAppProcesses();

		for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {
			if (appProcessInfo.pid == aPID) {
				int[] tMempid = new int[] { aPID };
				Debug.MemoryInfo[] tInfo = mActivityManager.getProcessMemoryInfo(tMempid);

				return tInfo[0].getTotalPss();
			}
		}
		return 0;
	}

	/**
	 * 获取当前系统语言编码, 暂时只识别简体中文和英文, 默认返回中文id
	 * @return
	 */
	public static short getLanguageCode() {
		String country = App.GetInstance().getResources().getConfiguration().locale.getCountry();
        
        if("CN".equals(country)){
        	return 2052;
        }else if("US".equals(country)){
        	return 1033;
        }
		return 2052;
	}
}
